
$(function(){
	//添加
	$('#addBtn').on('click',function(){
		var _that = $(this);
		var controlGroup = $("<div class='control-group removeFlag'></div>");
		var controls = $('<div  class="controls"></div>');
		var input1 = $('<input type="text" class="input-large promptBtn" flagInput="1"/>').val(_that.prev().prev().val());
		var input2 = $('<input type="text" class="input-large promptBtn" flagInput="2"/>').val(_that.prev().val());
		var span = $('<span class="help-inline btn btn-danger removeBtn">删除</span>');

		var controls2 = $('<div class="controls prompt"></div>');
		var controls2Span1 = $('<span style="display:inline-block;width:225px;"></span>').text(_that.parent().next().find('span:first').text());
		var controls2Span2 = $('<span style="display:inline-block;width:225px;"></span>').text(_that.parent().next().find('span:last').text());;
		controls2.append(controls2Span1);
		controls2.append(controls2Span2);

		controls.append(input1);
		controls.append(input2);
		controls.append(span);
		controlGroup.append(controls);
		controlGroup.append(controls2);
		$('#newGroup').append(controlGroup);
	})
	
	//删除
	$('#newGroup').delegate('.removeBtn','click',function(){
		var _that = $(this);
		_that.parents('.control-group').remove();
	})

	//根据目的地id获取目的地名
	$('#newGroup').delegate('.promptBtn','blur',function(){
		var _that=$(this);
		$.ajax({
			url:"/cms20/topic/getDestinationNameById",//todo
			data:{
				"destinationId":parseInt($.trim($(this).val()))
			},
			dataType:"json",
			type:"POST",
			success:function(data){
				if(data['status'] == '1'){
					if(_that.attr('flagInput')=='1'){
						_that.parent().next().find('span:first').text(data['desc']);
					}else if(_that.attr('flagInput')=='2'){
						_that.parent().next().find('span:last').text(data['desc']);
					}
				}
			}
		})
	})

	//修改接口
	$('#editThemeBtn').on('click',function(){
		var idArr=[];
		$('.promptBtn').each(function(){
			if($(this).val()!=''){
				idArr.push($(this).val());
			}
		})

		var dataList = {
			title:$('#title').val(),
			description:$('#description').val(),
			destinationIds:idArr.join(','),
			topicId:parent.document.getElementById("rightContent").getAttribute("themeid")
		};

		$.ajax({
			url:"/cms20/topic/save",//todo
			data:dataList,
			dataType:"json",
			type:"POST",
			success:function(data){
				if(data['status']=='1'){
					alert('成功');
				}else{
					alert(data['desc']);
				}
			}
		})
	})

	//返回主题列表页

	$('#backThemeList').on('click',function(){
		parent.document.getElementById("contIframe").setAttribute('src',"/cms20/jsp/cms/operate/themeList.html");
	})


	//添加接口
	$('#addThemeBtn').on('click',function(){
		var idArr=[];
		$('.promptBtn').each(function(){
			if($(this).val()!=''){
				idArr.push($(this).val());
			}
		})

		var dataList = {
			title:$('#title').val(),
			description:$('#description').val(),
			destinationIds:idArr.join(',')
		};

		$.ajax({
			url:"/cms20/topic/save",
			data:dataList,
			dataType:"json",
			type:"POST",
			success:function(data){
				if(data['status']=='1'){
					alert('成功');
				}else{
					alert(data['desc']);
				}
			}
		})
	})

})





//获取并渲染数据
function getData(){
		$.ajax({
			url:"/cms20/topic/getTopic",//todo
			data:{
				"topicId":parent.document.getElementById("rightContent").getAttribute("themeid")
			},
			dataType:"json",
			type:"POST",
			success:function(data){
				console.log(data);
				//渲染数据
				$('#title').val(data['title']);
				$('#description').val(data['description']);
				
				var ids = JSON.parse(data['destinationIds']);
				console.log(ids);
				for(var i=0;i<ids.length;i++){
					var controlGroup = $("<div class='control-group removeFlag'></div>");
					var controls = $('<div  class="controls"></div>');
					var input1 = $('<input type="text" class="input-large promptBtn" flagInput="1"/>').val(ids[i][0]);
					var input2 = $('<input type="text" class="input-large promptBtn" flagInput="2"/>').val(ids[i][1]);
					var span = $('<span class="help-inline btn btn-danger removeBtn">删除</span>');

					var controls2 = $('<div class="controls prompt"></div>');
					var controls2Span1 = $('<span style="display:inline-block;width:225px;"></span>');
					var controls2Span2 = $('<span style="display:inline-block;width:225px;"></span>');
					controls2.append(controls2Span1);
					controls2.append(controls2Span2);

					controls.append(input1);
					controls.append(input2);
					controls.append(span);
					controlGroup.append(controls);
					controlGroup.append(controls2);
					$('#newGroup').append(controlGroup);
				}
			}
		})
	}